//
//  VRContactCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/13.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRContactCell.h"

#import "VRContactModel.h"

@interface VRContactCell()


@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UILabel *nameLbl;

@property (nonatomic, weak) UIButton *callBtn;

@property (nonatomic, assign) UIView *bottomLineView;

@end

@implementation VRContactCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"VRContactCell";
    
    VRContactCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:@"icon"];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:iconImgView];
    self.iconImgView = iconImgView;
    
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.textColor = RGB(51, 51, 51);
    nameLbl.font = VRFont(16);
    [self.contentView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    UIButton *callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    callBtn.backgroundColor = RGB(78, 173, 57);
    callBtn.titleLabel.font = VRFont(16);
    [callBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [callBtn setTitle:@"拨打" forState:UIControlStateNormal];
    [callBtn setImage:[UIImage imageNamed:@"call"] forState:UIControlStateNormal];
    callBtn.layer.cornerRadius = 2;
    [callBtn addTarget:self action:@selector(onCall) forControlEvents:UIControlEventTouchUpInside];
    
    callBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, -3);
    callBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 3);
    [self.contentView addSubview:callBtn];
    self.callBtn = callBtn;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(24);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.callBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(82);
        make.height.mas_equalTo(30);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
}


#pragma mark - action
- (void)onCall
{
    
    NSString *phoneNum = self.model.fPhone;
    if (phoneNum.length > 0) {
        
        NSMutableString *numberStr= [[NSMutableString alloc] initWithFormat:@"telprompt://%@", phoneNum];
        //        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberStr]];
        /// 解决iOS10及其以上系统弹出拨号框延迟的问题
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
            /// 10及其以上系统
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberStr] options:@{} completionHandler:nil];
            } else {
                // Fallback on earlier versions
            }
        } else {
            /// 10以下系统
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberStr]];
        }
    }
}


#pragma mark - setter && getter
- (void)setModel:(VRContactModel *)model
{
    _model = model;
    
    self.nameLbl.text = model.fName;
    
    
}

@end
